import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class NavigatorUtils {
  ///替换
  static pushReplacementNamed(BuildContext context, String routeName) {
    Navigator.pushReplacementNamed(context, routeName);
  }

//  static homeToTop(BuildContext context) {
//    Navigator.of(context).pushNamedAndRemoveUntil(HomePage.sName, (Route<dynamic> route) => false);
//  }

  static pushAndPopUntil(BuildContext context,Widget widget,String routeName){
    Navigator.of(context).pushAndRemoveUntil(new CupertinoPageRoute(builder: (context)=>widget), (route)=>route.settings.name == routeName);
  }

  static pageToTop(BuildContext context,Widget widget){
    Navigator.pushAndRemoveUntil(context,
        MaterialPageRoute(builder: (_) => widget), (Route router) => false);
  }

//  static loginToTop(BuildContext context) {
//    Navigator.of(context).pushNamedAndRemoveUntil(FinalLoginPage.sName, (Route<dynamic> route) => false);
//  }

  ///切换无参数页面
  static pushNamed(BuildContext context, String routeName) {
    Navigator.pushNamed(context, routeName);

  }

  ///主页
//  static goHome(BuildContext context) {
//    Navigator.pushReplacementNamed(context, HomePage.sName);
//  }

  ///登录页
//  static goLogin(BuildContext context) {
//    Navigator.pushReplacementNamed(context, FinalLoginPage.sName);
//  }

  ///全屏Web页面
//  static Future goWebView(BuildContext context, String url, String title) {
//    return navigatorRouter(context, new MyWebView(url, title));
//  }

  ///图片预览
//  static gotoPhotoViewPage(BuildContext context, String url) {
//    navigatorRouter(context, new PhotoViewPage(url));
//  }

  ///isSwipeBack：true滑动屏幕边缘退出界面，false禁止滑动退出
  static Future navigatorRouter(BuildContext context, Widget widget, [bool isSwipeBack = true]) {
    FocusScope.of(context).requestFocus(new FocusNode());// 返回不让键盘弹出
    var pageRoute = isSwipeBack
        ? MaterialPageRoute(builder: (context) => widget, settings: RouteSettings(name: widget.toString()))
        : _DisableSwipeBackPageRoute(builder: (context) => widget, settings: RouteSettings(name: widget.toString()));
    return Navigator.push(context, pageRoute);
  }

  static Future navigatorReplaceRouter(BuildContext context, Widget widget, [bool isSwipeBack = true]) {
    var pageRoute = isSwipeBack
        ? MaterialPageRoute(builder: (context) => widget, settings: RouteSettings(name: widget.toString()))
        : _DisableSwipeBackPageRoute(builder: (context) => widget, settings: RouteSettings(name: widget.toString()));
    return Navigator.pushReplacement(context, pageRoute);
  }

  //追加一个页面来显示
  static Future materialPageRouteTo(BuildContext context, Widget widget) {
    Navigator.push(
      context,
      new MaterialPageRoute(builder: (context) => widget),
    );
  }

}

//禁用侧滑关闭
class _DisableSwipeBackPageRoute extends MaterialPageRoute {
  @protected
  bool get hasScopedWillPopCallback {
    return true;
  }

  _DisableSwipeBackPageRoute({
    @required WidgetBuilder builder,
    RouteSettings settings,
    bool maintainState = true,
    bool fullscreenDialog = false,
  }) : super(
    builder: builder,
    settings: settings,
    maintainState: maintainState,
    fullscreenDialog: fullscreenDialog,
  );

}
